package com.genersoft.iot.vmp.task;

import com.alibaba.fastjson.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;

public class DetectMessageList {

    private final static Logger logger = LoggerFactory.getLogger(DetectMessageList.class);
    private static BlockingQueue<JSONObject> basket = new ArrayBlockingQueue<JSONObject>(100);

    // 生产消息
    public static void produce(JSONObject json) throws InterruptedException{
        // put方法放入一个消息，若basket满了，等到basket有位置
        //basket.put(json);
        try {
            basket.add(json);
        } catch (Exception e) {
            logger.info("detect list is full");
        }

    }
    // 消费消息，从篮子中取走
    public static JSONObject consume() throws InterruptedException{
        // get方法取出一个消息，若basket为空，等到basket有消息为止
        JSONObject apple = basket.take();
        return apple;
    }

    public static int getMessageNumber(){
        return basket.size();
    }
}
